#mapping-script
# -*- coding: ms949 -*-
import bots.transform as transform
import bots.usersys.mappings.utils as utils

def main(inn,out):
    
    ns1 = '{urn:kr:gov:kcs:data:standard:KCS_NoticeOfImportGoodsAmendmentResultSchemaModule:1:0:0}'
    ns2 = '{urn:kr:gov:kcs:common:standard:KCS_ReusableAggregateBusinessInformationEntitySchemaModule:1:0:0}'
    rootelement = ns1 + 'KCS_NoticeOfImportGoodsAmendmentResult'
    
    #Read from inn by XML order

    #KCS_NoticeOfImportGoodsAmendmentResult  
    AcceptanceDate = inn.get({'BOTSID':rootelement,ns1+'AcceptanceDate':None})
    TypeCode = inn.get({'BOTSID':rootelement,ns1+'TypeCode':None})
    AmendmentDegreeNumeric = inn.get({'BOTSID':rootelement,ns1+'AmendmentDegreeNumeric':None})
    RejectReason = inn.get({'BOTSID':rootelement,ns1+'RejectReason':None})
    
    #KCS_NoticeOfImportGoodsAmendmentResult/Document
    DocumentTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'TypeCode':None})
    DocumentReferenceID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'referenceID':None})
    DocumentIssueDateTime = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'Document',ns2+'IssueDateTime':None})
    
    #KCS_NoticeOfImportGoodsAmendmentResult/CustomsOffice
    CustomsOfficeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'CustomsOffice',ns2+'OfficeCode':None})
    CustomsOfficeCodeID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'CustomsOffice',ns2+'CodeID':None})
    
    #KCS_NoticeOfImportGoodsAmendmentResult/CustomsOffice/CustomsOfficer
    CustomsOfficeCustomsOfficer = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'CustomsOffice'},{'BOTSID':ns2+'CustomsOfficer',ns2+'Name':None})
    
    #KCS_NoticeOfImportGoodsAmendmentResult/PreviousCustomsDocument
    PreviousCustomsDocumentTypeCode = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'PreviousCustomsDocument',ns2+'TypeCode':None})
    PreviousCustomsDocumentReferenceID = inn.get({'BOTSID':rootelement},{'BOTSID':ns1+'PreviousCustomsDocument',ns2+'referenceID':None})
    
    
    out.ta_info['botskey'] = 'CUSRES-5LG-' + PreviousCustomsDocumentReferenceID.strip()
    
    #Write to out by EDIFACT order
    
    out.put({'BOTSID':'UNH','0062':out.ta_info['reference']})
    out.put({'BOTSID':'UNH','S009.0065':'CUSRES'})
    out.put({'BOTSID':'UNH','S009.0052':'S'})
    out.put({'BOTSID':'UNH','S009.0054':'93A'})
    out.put({'BOTSID':'UNH','S009.0051':'UN'})
            
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','C002.1001':DocumentTypeCode})
    out.put({'BOTSID':'UNH'},{'BOTSID':'BGM','1004':PreviousCustomsDocumentReferenceID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'NAD','3035':'CM'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'NAD','C058.3124#1':CustomsOfficeCustomsOfficer})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'LOC','3227':'41','C517.3225':CustomsOfficeCodeID,'C517.1131':'113','C517.3055':'KCS'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'LOC','C519.3223':CustomsOfficeCode,'C519.1131':'5AB','C519.3055':'KCS'})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'187','C507.2380':utils.edifactYYYYMMDD(AcceptanceDate),'C507.2379':'102'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'DTM','C507.2005':'184','C507.2380':utils.edifactYYYYMMDDHHNN(DocumentIssueDateTime),'C507.2379':'203'})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'GIS','C529.7365':TypeCode})
    out.put({'BOTSID':'UNH'},{'BOTSID':'GIS','C529.1131':'5AI'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'GIS','C529.3055':'KCS'})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'FTX','4451':'AAI','C108.4440#1':AmendmentDegreeNumeric})
    
    if RejectReason != None:
        out.put({'BOTSID':'UNH'},{'BOTSID':'FTX','4451':'ACD','C108.4440#1':RejectReason})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'RFF','C506.1153':'ACE'})
    out.put({'BOTSID':'UNH'},{'BOTSID':'RFF','C506.1154':PreviousCustomsDocumentReferenceID})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'DOC','C002.1001':PreviousCustomsDocumentTypeCode})
    
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0074':out.getcount()+1})
    out.put({'BOTSID':'UNH'},{'BOTSID':'UNT','0062':out.ta_info['reference']}) 
    
